
// Source File Name:   BLCommonImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.ST;



//            ANYImpl, BLImpl, STImpl

public abstract class BLCommonImpl extends ANYImpl
    implements BL
{

    public static final String RCS_ID = "$Header: BLCommonImpl.java 1.0 2006/07/13 15:04:14 pkiran noship $";
    protected static final ST szTrue = STImpl.valueOf("true");
    protected static final ST szFalse = STImpl.valueOf("false");
    protected boolean _value;

    public BLCommonImpl(boolean flag)
    {
        _value = flag;
    }

    public BLCommonImpl(CS cs)
    {
        super(true, cs);
        _value = false;
    }

    public BL and(BL bl)
    {
        if(bl == null)
            throw ANYImpl.newNullException("BL", "and", "other");
        if(isNull().booleanValue())
        {
            if(bl.isNull().booleanValue())
                return this;
            if(bl.isTrue())
                return this;
            else
                return bl;
        }
        if(isTrue())
            return bl;
        else
            return this;
    }

    public BL not()
    {
        if(isNull().booleanValue())
            return this;
        else
            return BLImpl.valueOf(!_value);
    }

    public BL or(BL bl)
    {
        if(bl == null)
            throw ANYImpl.newNullException("BL", "or", "other");
        if(isNull().booleanValue())
        {
            if(bl.isNull().booleanValue())
                return this;
            if(bl.isTrue())
                return bl;
            else
                return this;
        }
        if(isTrue())
            return this;
        else
            return bl;
    }

    public BL xor(BL bl)
    {
        return or(bl).and(and(bl).not());
    }

    public BL implies(BL bl)
    {
        return not().or(bl);
    }

    public ST literal()
    {
        if(isTrue())
            return szTrue;
        if(isFalse())
            return szFalse;
        else
            return STImpl.nullST(null);
    }

    public boolean isTrue()
    {
        return !isNull().booleanValue() && _value;
    }

    public boolean isFalse()
    {
        return !isNull().booleanValue() && !_value;
    }

    protected static boolean toBoolean(String s)
    {
        boolean flag;
        if(s != null && s.equalsIgnoreCase(szTrue.stringValue()))
            flag = true;
        else
        if(s != null && s.equalsIgnoreCase(szFalse.stringValue()))
            flag = false;
        else
            throw ANYImpl.newBooleanException(s);
        return flag;
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
//            xmlelement.setAttribute("value", isTrue() ? "true" : "false");
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

}
